#/bin/bash
#subsetsum_random_4_5_without_unary.cnf_b16_t1_j128_stAddjobstoassumptions.csv
#PREFIX="subsetsum_random_4_5_without_unary.cnf"
PREFIX=$1
#_b8_t1_j1_stPre-processing"
SUFFIX=".csv"

OUTPUT_FILE=$PREFIX$SUFFIX

function STATS_NAME() {
case $1 in
1) echo "Totaljobs"
;;
2) echo "Pre-processing"
;;
3) echo "Decision"
;;
4) echo "Conflictanalyzing"
;;
5) echo "Backtracking"
;;
6) echo "Structuresreset"
;;
7) echo "Creatingstructures"
;;
8) echo "Nextjob"
;;
9) echo "Addjobstoassumptions"
;;
10) echo "Processingresults"
;;
*) echo "Unknown option"
exit 1;

esac
}

function STATS_NUMBER() {

case ${1} in
"Total job's") return 1
;;
"Pre-processing") return 2
;;
"Decision") return 3
;;
"Conflict analyzing") return 4
;;
"Backtracking") return 5
;;
"Structures reset") return 6
;;
"Creating structures") return 7
;;
"Next job") return 6
;;
"Add jobs to assumptions") return 9
;;
"Processing results") return 10
;;
*) echo "Unknown option"
exit 1;
esac

}

function STATS_COLUMN() {

case ${1} in
"1" | "Total job's") echo "B"
;;
"2" | "Pre-processing") echo "D"
;;
"3" | "Decision") echo "F"
;;
"4" | "Conflict analyzing") echo "H"
;;
"5" | "Backtracking") echo "J"
;;
"6" | "Structures reset") echo "L"
;;
"7" | "Creating structures") echo "N"
;;
"8" | "Next job") echo "P"
;;
"9" | "Add jobs to assumptions") echo "R"
;;
"10" | "Processing results") echo "T"
;;
*) echo "Unknown option"
exit 1;
esac

}

function BLOCKS_LINE() {

case ${1} in
1) return 5
;;
2) return 13
;;
4) return 23
;;
8) return 35
;;
16) return 51
;;
32) return 75
;;
*) echo "Unknown option"
exit 1;
esac

}


echo "Output file = "$OUTPUT_FILE
BLOCK_CONFIGURATIONS=6

#NUMBER_OF_THREADS=1
NUMBER_OF_THREADS=2

for block in 1 2 4 8 16 #32
do

	BLOCKS_LINE $block
	LINE_NUMBER=$?

	HEADER1=
	HEADER2=
	HEADER3=
	HEADER4=

	let AMOUNT=$block*$NUMBER_OF_THREADS

#	for i in $(seq 0 $block)
	for i in $(seq 0 $AMOUNT)
        do
               DATA[$i]=
        done

        AVERAGE=
        STD_DEV=
        MAX=
	MIN=


	for stat in $(seq 1 10)
	do
	ST_NAME=`STATS_NAME $stat`


		for file in ${PREFIX}_b${block}_t${NUMBER_OF_THREADS}_j*_st${ST_NAME}${SUFFIX}
		do

			CURRENT_BLOCK=0
			CURRENT_THREAD=0

#			let LAST_THREAD_ID=$NUMBER_OF_THREADS-1
			while read line
			do
				INDEX=$CURRENT_BLOCK*${NUMBER_OF_THREADS}+$CURRENT_THREAD
				DATA[$INDEX]=${DATA[$INDEX]}"\"Block $CURRENT_BLOCK, thread $CURRENT_THREAD\",$line"
#				echo ${DATA[$INDEX]}

				let CURRENT_THREAD++
				if [[ "$CURRENT_THREAD" == "$NUMBER_OF_THREADS" ]]
				then
					let CURRENT_BLOCK++
					let CURRENT_THREAD=0
				fi

				if [[ $stat != "10" ]]
				then
					DATA[$INDEX]=${DATA[$INDEX]}","
				fi
			done < $file

			TYPE=`echo $file | sed s/"${PREFIX}_b${block}_t${NUMBER_OF_THREADS}_j"//g | cut -d "_" -f 2 | sed -r s/^"st"//g | cut -d "." -f 1`
#			OUTPUT_FILE=${PREFIX}_st${TYPE}${SUFFIX}
#			echo $file
			HEADER1="${HEADER1}File,$PREFIX"
			HEADER2="${HEADER2}Blocks,$block"
			HEADER3="${HEADER3}Threads,${NUMBER_OF_THREADS}"
			HEADER4="${HEADER4}$ST_NAME,"

			if [[ $stat != "10" ]]
			then
				HEADER1=${HEADER1}","
				HEADER2=${HEADER2}","
				HEADER3=${HEADER3}","
				HEADER4=${HEADER4}","
			fi

			let START_LINE=$LINE_NUMBER+4

			let LAST_LINE=$LINE_NUMBER+${block}+3

			COLUMN=`STATS_COLUMN $stat`

			if [[ "$LAST_LINE" == "$START_LINE" ]]
			then
				RANGE=${COLUMN}$START_LINE
			else
				RANGE="${COLUMN}${START_LINE}:${COLUMN}${LAST_LINE}"
			fi

			AVERAGE=$AVERAGE"," #"Average,=(AVERAGE($RANGE))"
			STD_DEV=$STD_DEV"," #"Standard deviation,=stdev($RANGE)"

			MAX=$MAX"," #"Max,\"=max($RANGE)\""
			MIN=$MIN"," #"Min,=min($RANGE)"

			if [[ $stat != "10" ]]
			then
				AVERAGE=$AVERAGE","
				STD_DEV=$STD_DEV","
				MAX=$MAX","
				MIN=$MIN","
			fi

			rm $file

		done
	done
	echo $HEADER1 >> $OUTPUT_FILE
	echo $HEADER2 >> $OUTPUT_FILE
	echo $HEADER3 >> $OUTPUT_FILE
	echo $HEADER4 >> $OUTPUT_FILE

#	let CURRENT_BLOCK--
	let AMOUNT=$block*$NUMBER_OF_THREADS-1

	for i in $(seq 0 $AMOUNT)
        do
               echo ${DATA[$i]} >> $OUTPUT_FILE
        done

#	for i in $(seq 0 $CURRENT_BLOCK)
#	do
#		echo ${DATA[$i]} >> $OUTPUT_FILE
#	done

#	echo $AVERAGE >> $OUTPUT_FILE 
#	if [[ "$block" != "1" ]]
#	then
#		echo $STD_DEV >> $OUTPUT_FILE
#	fi

#       echo $MAX >> $OUTPUT_FILE
#       echo $MIN >> $OUTPUT_FILE


done
